<?php

namespace Project\PlanningPokerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Project\PlanningPokerBundle\Classes\NoteParser;
use Project\PlanningPokerBundle\Classes\MessageParser;

class PlanningPokerController extends Controller {

    /**
     * Action lancé par défault
     * @return type
     */
    public function indexAction() {
        $session = $this->get('session');
        //Si l'utilisateur est le scum master ou un dev, on affiche le planning poker
        if ($session->get('role') == "ROLE_SCRUMMASTER" || $session->get('role') == "ROLE_DEV") {
            $noteParser = new NoteParser();
            $note = $noteParser->getMyNote($session->get('email'));
            $us = $this->getUserStory();
            return $this->render('ProjectPlanningPokerBundle:PlanningPoker:index.html.twig', array('currentNote' => $note, 'userstory' => $us));
        } else {
            return $this->redirect($this->generateUrl('project_authentification_login'));
        }
    }

    /**
     * Rafraichi constament la vue !
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function refreshAction() {
        $noteParser = new NoteParser();
        $notes = $noteParser->getNotes();

        $messageParser = new MessageParser();
        $messages = $messageParser->getMessages();

        $us = $this->getUserStory();

        $return = json_encode(array("note" => $notes, "userstory" => $us->getRole(), "message" => $messages));
        return new Response($return, 200, array('Content-Type' => 'application/json'));
    }

    /**
     * Envoi la note dans le fichier texte
     * @param type $note
     * @return type
     */
    public function sendNoteAction($note) {
        $session = $this->get('session');
        //Si l'utilisateur est le scum master ou un dev, on ajoute la note
        if ($session->get('role') == "ROLE_SCRUMMASTER" || $session->get('role') == "ROLE_DEV") {
            $noteParser = new NoteParser();
            $noteParser->addLine($session->get('email'), $note, true);
            return $this->redirect($this->generateUrl('project_planning_poker_index'));
        } else {
            return $this->redirect($this->generateUrl('project_authentification_login'));
        }
    }

    public function sendMessageAction() {
        $session = $this->get('session');
        //Si l'utilisateur est le scum master ou un dev, on ajoute le message
        if ($session->get('role') == "ROLE_SCRUMMASTER" || $session->get('role') == "ROLE_DEV") {
            $message = $this->getRequest()->request->get('enterMessage');
            $messageParser = new MessageParser();
            $messageParser->addLine($session->get('email'), $message, false);
            return $this->redirect($this->generateUrl('project_planning_poker_index'));
        } else {
            return $this->redirect($this->generateUrl('project_authentification_login'));
        }
    }

    /**
     * Supprime toutes les notes du fichier texte afin de pouvoir renoter
     */
    public function reassessAction() {
        $session = $this->get('session');
        //Si l'utilisateur est le scum master, on supprime toutes les notes
        if ($session->get('role') == "ROLE_SCRUMMASTER") {
            $noteParser = new NoteParser();
            $noteParser->clearFile();
            return $this->redirect($this->generateUrl('project_planning_poker_index'));
        } else {
            return $this->redirect($this->generateUrl('project_authentification_login'));
        }
    }

    /**
     * Enregistre les points de difficultés pour la user story concerné
     * @param type $note
     */
    public function validateNoteAction($note) {
        $session = $this->get('session');
        //Si l'utilisateur est le scum master, on enregistre la note pour la user story
        if ($session->get('role') == "ROLE_SCRUMMASTER") {
            $us = $this->getUserStory();
            $us->setPtsDifficulte($note);

            $em = $this->getDoctrine()->getManager();
            $em->persist($us);
            $em->flush();
            $noteParser = new NoteParser();
            $noteParser->clearFile();
            $messageParser = new MessageParser();
            $messageParser->clearFile();
            return $this->redirect($this->generateUrl('project_planning_poker_index'));
        } else {
            return $this->redirect($this->generateUrl('project_authentification_login'));
        }
    }

    /**
     * Renvoi la dernière user story non noté de la db
     * @return type
     */
    private function getUserStory() {
        $manager = $this->getDoctrine()->getManager();
        $userstories = $manager->getRepository('ProjectScrumBundle:Userstories')->findBy(array('ptsDifficulte' => 0));
        $us = "";
        if (!empty($userstories)) {
            $us = $userstories[0];
        }
        return $us;
    }

}